﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Web;
using FS.Business;
using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Info_SpecialForm : PageBase
    {
        private string _key;
        visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            
            if (!base.IsPostBack)
            {
                this.GetSelect_TrustInfo();
                this.GetSelect_Country();
                this.GetSelect_Certtype();
                this.GetSelect_Supply();

                //设置签证时间
                this.info_getdate.Value = DateTime.Now.ToString("yyyy-MM-dd");

                string infoid = CommonHelper.GetGuid;
                if (!string.IsNullOrEmpty(this._key))
                {
                    ShowMsgHelper.Alert_Error("暂时不支持编辑操作！");
                    infoid = this._key;
                    //this.InitData();
                }
                this.Hinfo.Value = infoid;
                DataBindGrid();
            }
        }

        #region 下拉框信息
        //委托方
        private void GetSelect_TrustInfo()
        {
            DataTable dt = _visa.GetSelect_TrustInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.trust_id, "name", "trust_id", "");
        }

        //所属国家
        private void GetSelect_Country()
        {
            DataTable dt = _visa.GetSelect_CityInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.select_country, "city_name", "city_id", "请选择");
        }

        //签证类型
        private void GetSelect_Certtype()
        {
            DataTable dt = _visa.GetSelect_CertType();
            ControlBindHelper.BindHtmlSelect(dt, this.select_certtype, "property_value", "property_id", "请选择");
        }

        //供应商
        private void GetSelect_Supply()
        {
            DataTable dt = _visa.GetSelect_SupplyInfoSimple();
            ControlBindHelper.BindHtmlSelect(dt, this.select_supply, "Supply_Name", "Supply_id", "请选择");
        }
        #endregion

        private void DataBindGrid()
        {
            string ls_infotid = this.Hinfo.Value.Trim();
            DataTable dt = _visa.GetInfoDetail(ls_infotid);
            DataTable dtsum = _visa.GetTotalInfoDetail(ls_infotid);
            string ls_people = "0";
            string ls_outmoney = "0";            
            string ls_moneyout = "0";

            if (dtsum.Rows.Count > 0)            
            {
                ls_people = dtsum.Rows[0]["totalpeo"].ToString();                
                ls_outmoney = dtsum.Rows[0]["price"].ToString();
                ls_moneyout = dtsum.Rows[0]["cost"].ToString();
            }

            this.info_outmoney.Value = ls_outmoney;
            this.info_outpeople.Value = ls_people;
            this.info_moneyin.Value = ls_outmoney;
            this.info_moneyout.Value = ls_moneyout;
            ControlBindHelper.BindRepeaterList(dt, this.rp_Item);
        }

        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtdel_Click(object sender, EventArgs e)
        {
            string[] ls_detailid = this.HDetailID.Value.Split(',');
            if (ls_detailid.Length>0)
            {
                if (!_visa.DeleteInfoDetailBatch(ls_detailid))
                {
                    ShowMsgHelper.Alert_Error("客户删除失败！");
                }
                else
                {
                    this.DataBindGrid();
                }
            }
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtadd_Click(object sender, EventArgs e)
        {
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_username= RequestSession.GetSessionUser().UserName.ToString();
            string ls_infoid = this.Hinfo.Value.Trim();
            string ls_name = this.detail_customer.Value.Trim();
            string ls_cprice = this.select_Cprice.Value.Trim();
            string ls_sprice = this.select_Sprice.Value.Trim();
            string ls_Countryid = this.HCountryid.Value;
            //string ls_CountryName = this.HCountryName.Value;
            string ls_CertTypeID = this.HCertTypeID.Value;
            string ls_CertTypeName = this.HCertTypeName.Value;
            string ls_SupplyID = this.HSupplyID.Value;
            string ls_SupplyName = this.HSupplyName.Value;

            decimal ldc_cprice = 0;
            decimal ldc_sprice = 0;

            #region 数据校验
            if (string.IsNullOrEmpty(ls_Countryid))
            {
                ShowMsgHelper.showWarningMsg("请选择签证国家！");
                return;
            }
            if (string.IsNullOrEmpty(ls_CertTypeID))
            {
                ShowMsgHelper.showWarningMsg("请选择签证类型！");
                return;
            }
            if (string.IsNullOrEmpty(ls_name))
            {
                ShowMsgHelper.showWarningMsg("请输入客户名称！");
                return;
            }
            if (string.IsNullOrEmpty(ls_SupplyID))
            {
                ShowMsgHelper.showWarningMsg("请选择供应商！");
                return;
            }
            if (!decimal.TryParse(ls_cprice, out ldc_cprice))
            {
                ShowMsgHelper.showWarningMsg("请输入签证金额！");
                return;
            }

            if (!decimal.TryParse(ls_sprice, out ldc_sprice))
            {
                ShowMsgHelper.showWarningMsg("请输入供应商成本！");
                return;
            }            
            
            //if (string.IsNullOrEmpty(ls_cprice))
            //{
            //    ShowMsgHelper.showWarningMsg("请输入签证金额！");
            //    return;
            //}

            //if (string.IsNullOrEmpty(ls_sprice))
            //{
            //    ShowMsgHelper.showWarningMsg("请输入供应商成本！");
            //    return;
            //}
            #endregion            

            //插入下拉数据
            if (ls_CertTypeID == "s")   //新建签证类型
            {
                //插入签证信息
                ls_CertTypeID=_visa.Info_InsertCertType( ls_CertTypeName, ls_userid, ls_username);
                this.HCertTypeID.Value = ls_CertTypeID;

                //插入国家签证信息
                _visa.Info_InsertCertCity(ls_Countryid, ls_CertTypeID, ls_userid, ls_username,ldc_cprice);
            }

            if (ls_SupplyID == "s")   
            {
                //插入供应商
                ls_SupplyID = _visa.Info_InsertSupply(ls_SupplyName, ls_userid, ls_username);
                this.HSupplyID.Value = ls_SupplyID;

                //插入供应商报价
                _visa.Info_SupplyPrice(ls_SupplyID, ls_Countryid, ls_CertTypeID, ldc_sprice, ls_userid, ls_username);
            }

            string ls_msg = _visa.InfoDetail_insertspecial(ls_infoid, ls_name, ls_Countryid, ls_CertTypeID,
                ls_SupplyID, ldc_cprice, ldc_sprice, ls_userid, ls_username);
            string[] ls_array = ls_msg.Split('@');
            if (ls_array[0]=="0")
            {               
                this.DataBindGrid();
                this.detail_customer.Focus();                
            }
            else
            {
                ShowMsgHelper.Alert_Error(ls_array[1]);
            }
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            //ht = ControlBindHelper.GetWebControls(this.Page);
            string ls_infoid = this.Hinfo.Value.Trim();
            string ls_people = this.info_outpeople.Value;
            string ls_userid = RequestSession.GetSessionUser().UserId.ToString();
            string ls_username=RequestSession.GetSessionUser().UserName.ToString();

            if (ls_people == "0")
            {
                ShowMsgHelper.Alert_Error("请填写客户信息！");
                return;
            }

            if (!string.IsNullOrEmpty(this._key))
            {
                ht["ModifyDate"] = DateTime.Now;
                ht["ModifyUserId"] = ls_userid;
                ht["ModifyUserName"] = ls_username;
            }
            else
            {
                ht["Info_ID"] = ls_infoid;
                ht["CreateUserId"] = ls_userid;
                ht["CreateUserName"] = ls_username;
            }

            string ls_trust=this.HCustomerid.Value;
            string ls_trustname=this.HCustomerName.Value;
            if (ls_trust == "s")   
            {
                //插入供应商
                ls_trust = _visa.Info_InsertCustomer(ls_trustname, ls_userid, ls_username);
                this.HCustomerid.Value = ls_trust;
            }

            ht["info_getdate"] = this.info_getdate.Value;
            ht["info_remark"] = this.info_remark.Value;
            ht["trust_id"] = ls_trust;
            ht["info_outpeople"] = ls_people;
            ht["info_outmoney"] = this.info_outmoney.Value;
            ht["info_moneyin"] = this.info_moneyin.Value;
            ht["info_moneyout"] = this.info_moneyout.Value;

            bool IsOk = DataFactory.Run().Submit_AddOrEdit("Visa_Info", "Info_ID", this._key, ht);
            if (IsOk)
            {
                if (string.IsNullOrEmpty(this._key))
                {
                    _visa.INFO_SetInfoNoSpe(ls_infoid);
                }
                ShowMsgHelper.AlertMsg("操作成功！");
            }
            else
            {
                ShowMsgHelper.Alert_Error("操作失败！");
            }
        }
    }
}